import javax.swing.*;
import java.awt.*;
import java.awt.geom.Path2D;

class TriangleGUI {
    public static void showTriangleWindow() {
        JFrame frame = new JFrame("三角形计算器");
        frame.setSize(500, 400);
        frame.setLayout(new BorderLayout());

        // 输入面板
        JPanel inputPanel = new JPanel(new GridLayout(4, 2, 10, 10));
        inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JLabel baseLabel = new JLabel("底边:");
        JTextField baseField = new JTextField();
        JLabel heightLabel = new JLabel("高:");
        JTextField heightField = new JTextField();
        JLabel side1Label = new JLabel("边长1:");
        JTextField side1Field = new JTextField();
        JLabel side2Label = new JLabel("边长2:");
        JTextField side2Field = new JTextField();

        JButton calculateBtn = new JButton("计算");
        JButton drawBtn = new JButton("绘制图形");

        inputPanel.add(baseLabel);
        inputPanel.add(baseField);
        inputPanel.add(heightLabel);
        inputPanel.add(heightField);
        inputPanel.add(side1Label);
        inputPanel.add(side1Field);
        inputPanel.add(side2Label);
        inputPanel.add(side2Field);
        inputPanel.add(calculateBtn);
        inputPanel.add(drawBtn);

        // 结果面板
        JTextArea resultArea = new JTextArea();
        resultArea.setEditable(false);

        calculateBtn.addActionListener(e -> calculateTriangle(baseField, heightField, side1Field, side2Field, resultArea));
        drawBtn.addActionListener(e -> drawTriangle(baseField, heightField));

        frame.add(inputPanel, BorderLayout.NORTH);
        frame.add(new JScrollPane(resultArea), BorderLayout.CENTER);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static void calculateTriangle(JTextField baseField, JTextField heightField,
                                          JTextField side1Field, JTextField side2Field,
                                          JTextArea resultArea) {
        try {
            double base = Double.parseDouble(baseField.getText());
            double height = Double.parseDouble(heightField.getText());
            double side1 = Double.parseDouble(side1Field.getText());
            double side2 = Double.parseDouble(side2Field.getText());
            double side3 = base;

            double area = 0.5 * base * height;
            double perimeter = side1 + side2 + side3;

            String result = String.format("三角形计算结果:\n底边: %.2f\n高: %.2f\n边长1: %.2f\n边长2: %.2f\n面积: %.2f\n周长: %.2f",
                    base, height, side1, side2, area, perimeter);
            resultArea.setText(result);
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(null, "请输入有效的数字！");
        }
    }

    private static void drawTriangle(JTextField baseField, JTextField heightField) {
        try {
            double base = Double.parseDouble(baseField.getText());
            double height = Double.parseDouble(heightField.getText());
            // 打开单独的绘图窗口
            new TriangleDrawingWindow(base, height);
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(null, "请输入有效的数字！");
        }
    }
}

class TriangleDrawingWindow extends JFrame {
    private double base;
    private double height;

    public TriangleDrawingWindow(double base, double height) {
        this.base = base;
        this.height = height;
        initializeUI();
    }

    private void initializeUI() {
        setTitle("三角形图形 - 底边: " + base + " 高: " + height);
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        TriangleDrawingPanel drawingPanel = new TriangleDrawingPanel(base, height);
        add(drawingPanel);

        setVisible(true);
    }
}

class TriangleDrawingPanel extends JPanel {
    private double base;
    private double height;

    public TriangleDrawingPanel(double base, double height) {
        this.base = base;
        this.height = height;
        setBackground(Color.WHITE);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        // 启用抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        if (base > 0 && height > 0) {
            g2d.setColor(Color.GREEN);
            g2d.setStroke(new BasicStroke(3));

            int centerX = getWidth() / 2;
            int centerY = getHeight() / 2;

            // 缩放三角形以便显示
            int scaledBase = (int) (base * 8);
            int scaledHeight = (int) (height * 8);

            Path2D triangle = new Path2D.Double();
            triangle.moveTo(centerX - scaledBase/2, centerY + scaledHeight/2); // 左下角
            triangle.lineTo(centerX + scaledBase/2, centerY + scaledHeight/2); // 右下角
            triangle.lineTo(centerX, centerY - scaledHeight/2); // 顶点
            triangle.closePath();

            g2d.draw(triangle);

            // 标注尺寸
            g2d.setColor(Color.RED);
            g2d.setFont(new Font("微软雅黑", Font.BOLD, 14));
            g2d.drawString("底边: " + base, centerX - scaledBase/2, centerY + scaledHeight/2 + 25);
            g2d.drawString("高: " + height, centerX + scaledBase/2 + 10, centerY);

            // 绘制高线
            g2d.setColor(Color.BLUE);
            g2d.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{5}, 0));
            g2d.drawLine(centerX, centerY + scaledHeight/2, centerX, centerY - scaledHeight/2);
        }
    }
}